package help;

import colour.ColourUtil;
import java.util.StringTokenizer;

/**
 *
 * @author Michael Hanns
 *
 */
public class HelpEntry {

    private String entryTitle;
    private String entryText;
    private String[] entryKeywords;
    private String[] relatedEntries;

    public HelpEntry(String title) {
        entryTitle = title;
        entryText = "";
        entryKeywords = new String[0];
        relatedEntries = new String[0];
    }

    public void setText(String text) {
        int increment = 60;
        int line = 1;
        for (int x = 0; x < text.length(); x++) {
            if (x == (line * increment)) {
                text = text.substring((line - 1) * increment, line * increment)
                        + "\n"
                        + text.substring(increment * line);
            }
            x++;
            line++;
        }
        entryText = text;
    }

    public void setKeywords(String[] keywords) {
        entryKeywords = keywords;
    }

    public void setRelated(String[] related) {
        relatedEntries = related;
    }

    public boolean matchesSearch(String query) {
        String token = "";
        String totalQuery = "";
        StringTokenizer search = new StringTokenizer(query);
        while (search.hasMoreTokens()) {
            token = search.nextToken();
            if (totalQuery.length() == 0) {
                totalQuery = token;
            } else {
                totalQuery += " " + token;
            }
            for (int x = 0; x < entryKeywords.length; x++) {
                if (entryKeywords[x].equalsIgnoreCase(token)) {
                    return true;
                }
            }
        }

        if (entryTitle.equalsIgnoreCase(totalQuery)) {
            return true;
        }

        return false;

    }

    public String getEntry() {
        String entry = ColourUtil.colourise("----------------------------------------------------------------------------", ColourUtil.green)
                + ColourUtil.colourise("\nTitle   : ", ColourUtil.yellow) + ColourUtil.colourise(entryTitle, ColourUtil.white);

        if (entryKeywords.length > 0) {
            entry += ColourUtil.colourise("\nKeywords: ", ColourUtil.yellow) + ColourUtil.colourise("", ColourUtil.white);
            for (int x = 0; x
                    < entryKeywords.length; x++) {
                entry += entryKeywords[x].concat(" ");
            }
        }

        if (relatedEntries.length > 0) {
            entry += ColourUtil.colourise("\nRelated : ", ColourUtil.yellow) + ColourUtil.colourise("", ColourUtil.white);;
            for (int x = 0; x < relatedEntries.length; x++) {
                entry += relatedEntries[x] + " ";
            }
        }

        entry += "\n" + ColourUtil.colourise("----------------------------------------------------------------------------", ColourUtil.green);

        entry += "\n\n" + ColourUtil.colourise(entryText, ColourUtil.grey)
                + "\n" + ColourUtil.colourise("----------------------------------------------------------------------------", ColourUtil.green)
                +  ColourUtil.colourise("", ColourUtil.grey);
      
        
        return entry;
    }
}
